﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace qkview.Models
{
    public class SectionDto
    {
        public string SectionID { get; set; }
        public string NoteID { get; set; }
        [Required, MaxLength(50)]
        public string Title { get; set; }
        [MaxLength(50)]
        public string URLSlug { get; set; }
        [Required, MaxLength(500)]
        public string Summary { get; set; }
        public string Description { get; set; }
        public int SortIndex { get; set; }

        public SectionDto() { }

        public SectionDto(Section section)
        {
            SectionID = Common.Encode(section.SectionID.ToString());
            NoteID = Common.Encode(section.NoteID.ToString());
            Title = section.Title;
            Summary = section.Summary;
            Description = section.Description;
            SortIndex = section.SortIndex;
        }

        public Section ToEntity()
        {
            return new Section
            {
                SectionID = SectionID == null ? 0 : Convert.ToInt32(Common.Decode(SectionID)),
                NoteID = Convert.ToInt32(Common.Decode(NoteID)),
                Title = Title,
                URLSlug = URLSlug,
                Summary = Summary,
                Description = Description,
                SortIndex = SortIndex
            };
        }
    }

    public class NoteDto
    {
        public string NoteID { get; set; }
        [Required, MaxLength(50)]
        public string Title { get; set; }
        public string Description { get; set; }
        public int SortIndex { get; set; }
        public int? NoteTypeID { get; set; }
        public string BoardID { get; set; } //Encrypted BoardID
        public string ColorCode { get; set; }
        public virtual List<SectionDto> Sections { get; set; }

        public NoteDto() { }

        public NoteDto(Note note)
        {
            NoteID = Common.Encode(note.NoteID.ToString());
            Title = note.Title;
            Description = note.Description;
            SortIndex = note.SortIndex;
            BoardID = Common.Encode(note.BoardID.ToString());
            Sections = new List<SectionDto>();
            ColorCode = note.Theme == null ? "" : (note.Theme.ColorCode ?? "");
            NoteTypeID = note.NoteTypeID ?? 1;
            foreach (Section item in note.Sections)
            {
                Sections.Add(new SectionDto(item));
            }
        }



        public Note ToEntity()
        {

            Note note = new Note
            {
                NoteID = NoteID != null ? Convert.ToInt32(Common.Decode(NoteID)) : 0,
                Title = Title,
                Description = Description,
                SortIndex = SortIndex,
                NoteTypeID = NoteTypeID,
                BoardID = Convert.ToInt32(Common.Decode(BoardID)),
                Sections = new List<Section>()
            };
            foreach (SectionDto item in Sections)
            {
                note.Sections.Add(item.ToEntity());
            }

            return note;
        }


    }




}